function CrvaDetails() {
	this.flight = new Vol();
	this.unit = new Unite();
	this.aircraft = new Aeronef();
	this.aircraftType = new TypeAeronef();
	
	this.isNewFlight = false;

	this.loadCrva = function(flight) {
		this.flight = flight;
		!this.isNewFlight ? this.refresh() : "";
		this.generateTableMembers($("#members_list_tbody"));
	};

	this.generateTableSaeHours = function(domElement) {
		// Clean le contenu du tableau
		$("#edit_crme_hours_list_tbody").empty();
		for ( var i = 0; i < this.flight.listeHeuresSAE.length; i++) {

			var tr = document.createElement("tr");
			var tdCode, tdJour, tdNuit;
			tdCode = document.createElement("td");
			tdJour = document.createElement("td");
			tdNuit = document.createElement("td");

			crvapp.formUtil.addClassToElement(tdCode, "codeServiceAerien");
			crvapp.formUtil.addClassToElement(tdJour, "dureeVolJour");
			crvapp.formUtil.addClassToElement(tdNuit, "dureeVolNuit");
			tdCode.innerHTML = this.flight.listeHeuresSAE[i].serviceAerien;
			tdJour.innerHTML = "" + this.convertToHHMM(this.flight.listeHeuresSAE[i].dureeVolJour);
			tdNuit.innerHTML = "" + this.convertToHHMM(this.flight.listeHeuresSAE[i].dureeVolNuit);

			tr.appendChild(tdCode);
			tr.appendChild(tdJour);
			tr.appendChild(tdNuit);
			domElement.append(tr);
		}

		var _this = this;
		$("#edit_crme_hours_list_tbody").find("tr").each(function() {
			return $(this).on('click', function(e) {
				_this.editable_list_select($(this));
				return e.stopPropagation();
			});
		});

	};

	this.editable_list_select = function(div) {
		div = $(div);
		if (!div.hasClass('edit_table_selected_row')) {
			$('.edit_table_selected_row')
					.removeClass('edit_table_selected_row');
			return div.addClass('edit_table_selected_row');
		}
	};

	 this.convertToHHMM = function (timeInSeconds) {
		    var sec_num = parseInt(timeInSeconds, 10); // don't forget the second parm
		    var hours   = Math.floor(sec_num / 3600);
		    var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
		    var seconds = sec_num - (hours * 3600) - (minutes * 60);

		    if (hours   < 10) {hours   = "0"+hours;}
		    if (minutes < 10) {minutes = "0"+minutes;}
		    if (seconds < 10) {seconds = "0"+seconds;}
		    var time    = hours+':'+minutes; //+':'+seconds;
		    return time;
		};
	
	this.generateTableMembers = function(domElement) {
		$("#members_list_table").dataTable().fnClearTable();

		for ( var i = 0; i < this.flight.listeVolPersonnesBord.length; i++) {
			var tdnom = this.flight.listeVolPersonnesBord[i].personne.nom;
			var tdGrade = this.flight.listeVolPersonnesBord[i].personne.nomination.grade.grade;
			var tdFctn = this.flight.listeVolPersonnesBord[i].fonctionABord;
			var CRME = "...";
			$("#members_list_table").dataTable().fnAddData([ tdnom, tdGrade, tdFctn, CRME ]);
		}
	};

	this.getBackToDashboard = function() {
		crvapp.stockageUtil.exportXmlData();
		this.isNewFlight = false;
		
		//Delete the datatable object first
		if(flightTable != null) {
			flightTable.fnDestroy();
		}
		//Remove all the DOM elements
		$('#crva_list_tbody').empty();
		
		//Recreate the DOM Elements
		crvapp.dashboard.fillFlight(document.getElementById("crva_list_tbody"));
		
		//Re-create the datatable object
		flightTable = $("#crva_list_table").dataTable({
            "oLanguage": {
                "sUrl": "frenchDatatable.txt"
            },
            "iDisplayLength": 3,
            "bLengthChange" : false,
            "bFilter" : true,
            "bPaginate" : true,
            "bInfo" : true,
            "sScrollY" : "200px",
            "aoColumns" : [ {
                "bSortable" : false
            }, null, null, null, null, null, null, null, null ]
        });
		
		this.emptyCrvaDetails();
	};

	this.validate = function() {
		this.updateSelectedValues();
		this.isNewFlight ? crvapp.stockageUtil.listeVolsFull.push(this.flight) : "";
		crvapp.stockageUtil.setJsonValue("crvappData", crvapp.stockageUtil.listeVolsFull);
		this.isNewFlight = false;
		this.refresh();
	};
	
	this.emptyCrvaDetails = function() {
		$("#id_display").text("");
		$("#unit_display").text("");
		$("#state_display").text("");
		$("#category_display").text("");
		$("#aircraft_display").text("");
		$("#flightRule_display").text("");
		$("#flighType_display").text("");

		// OACI & GDH
		$("#oaci_begin_display").text("");
		$("#airfield_begin_display").text("");
		$("#oaci_end_display").text("");
		$("#airfield_end_display").text("");
		$("#gdh_begin_display").text("");
		$("#gdh_end_display").text("");
		$("#flight_time_total").text("");

		// Renseignements
		$("#nature_mission_display").text("");
		$("#manoeuvre_mission_display").text("");
		$("#type_mission_display").text("");
		
		$("#edit_crme_hours_list_tbody").empty();
	};

	this.refresh = function() {
		// Nav Bar
		$("#crva_detail_title").text(this.flight.indicatif);

		// Informations du vol
		$("#id_display").text(this.flight.indicatif);
		this.flight.unite != undefined ? $("#unit_display").text(this.flight.unite.nom.split('-')[this.flight.unite.nom.split('-').length - 1]) : "";
		$("#state_display").text(crvapp.dictionnary.transformFlightState(this.flight.etat));
		$("#category_display").text(crvapp.dictionnary.transformCategoryFlight(this.flight.categorie));
		$("#aircraft_display").text(this.flight.aeronef.typeAeronef.type);
		$("#flightRule_display").text(crvapp.dictionnary.transformFlightRule(this.flight.regleVol));
		$("#flighType_display").text(crvapp.dictionnary.transformFlightType(this.flight.typeVol));

		// OACI & GDH
		$("#oaci_begin_display").text(this.flight.listeDecollagesAtterrissages[0].lieuOACI.codeOaci);
		$("#airfield_begin_display").text(this.flight.listeDecollagesAtterrissages[0].lieuOACI.nom);
		$("#oaci_end_display").text(this.flight.listeDecollagesAtterrissages[this.flight.listeDecollagesAtterrissages.length - 1].lieuOACI.codeOaci);
		$("#airfield_end_display").text(this.flight.listeDecollagesAtterrissages[this.flight.listeDecollagesAtterrissages.length - 1].lieuOACI.nom);
		this.flight.gdhDebut != "" ? $("#gdh_begin_display").text(moment(this.flight.gdhDebut).format("DD-MM-YYYY HH:mm")) : "";
		this.flight.gdhFin != "" ? $("#gdh_end_display").text(moment(this.flight.gdhFin).format("DD-MM-YYYY HH:mm")) : "";
		$("#flight_time_total").text();

		// Renseignements
		$("#nature_mission_display").text(crvapp.dictionnary.transformMissionNature(this.flight.natureMission));
		$("#manoeuvre_mission_display").text(crvapp.dictionnary.transformMissionManoeuvre(this.flight.manoeuvreMission));
		$("#type_mission_display").text(crvapp.dictionnary.transformMissionType(this.flight.typeMission));

		// Consommation Carburant
		(typeof this.flight.masseCarburantConsommee) === "string" ? $("#fuel_consumption_display").text(this.flight.masseCarburantConsommee) : "0";
//		$("#fuel_consumption_display").text(this.flight.masseCarburantConsommee);
		var maxFuel = 550;
		var widthPercent = (this.flight.masseCarburantConsommee * 100) / maxFuel;
		$("#fuel_jauge_display").width(widthPercent + "%");

		this.generateTableSaeHours($("#edit_crme_hours_list_tbody"));
	};

	this.updateSelectedValues = function() {
		$edit = $('.demo_slide:visible');
		switch ($edit.attr("id")) {
		case "edit_flight_details":
			this.updateFlightDetails();
			break;
		case "edit_flight_consumption":
			this.updateFlightConsumption();
			break;
		case "edit_flight_intelligence":
			this.updateFlightIntelligence();
			break;
		case "edit_flight_trip":
			this.updateFlightTrip();
			break;
		case "edit_flight_hours":
			this.updateFlightHours();
			break;
		default:
			this.updateFlightDetails();
			this.updateFlightConsumption();
			this.updateFlightIntelligence();
			this.updateFlightTrip();
			this.updateFlightHours();
			break;
		}
	};

	this.updateFlightDetails = function() {
		$("#demo_text_editor").text() != this.flight.indicatif ? this.flight.indicatif = $("#demo_text_editor").text() : "";
		$("#unit_list_table").find("tr.active").first().length != 0 ? this.flight.unite = crvapp.stockageUtil.getUnitObjectById($("#unit_list_table").find("tr.active").first().attr("data-id"))
				: "";
		$("#flight_state_table").find("tr.active").first().children("td").first().text() != "" ? this.flight.etat = $("#flight_state_table").find("tr.active").first()
				.children("td").first().text() : "";
		$("#category_flight_table").find("tr.active").first().length != 0 ? this.flight.categorie = $("#category_flight_table").find("tr.active").first().children("td")
				.first().text() : "";
		$("#aeronef_flight_table").find("tr.active").first().length != 0 ? this.flight.aeronef = crvapp.stockageUtil.getAircraftObjectById($("#aeronef_flight_table").find(
				"tr.active").first().attr("data-id")) : "";
		$("#flight_rule_table").find("tr.active").first().length != 0 ? this.flight.regleVol = $("#flight_rule_table").find("tr.active").first().children("td").first().text()
				: "";
		$("#flight_type_table").find("tr.active").first().length != 0 ? this.flight.typeVol = $("#flight_type_table").find("tr.active").first().children("td").first().text()
				: "";
	};

	this.updateFlightConsumption = function() {
		$("#slider-carburantConsomme-data").text() != this.flight.masseCarburantConsommee ? this.flight.masseCarburantConsommee = $("#slider-carburantConsomme-data").text() : "";
	};
	
	this.updateFlightIntelligence = function() {
		var rowNature = fnGetSelected(natureMissionTable);
		rowNature.length > 0 ? this.flight.natureMission = rowNature.children().first().text() : "";
		var rowManoeuvre = fnGetSelected(manoeuvreMissionTable);
		rowManoeuvre.length > 0 ? this.flight.manoeuvreMission = rowManoeuvre.children().first().text() : "";
		var rowType = fnGetSelected(typeMissionTable);
		rowType.length > 0 ? this.flight.typeMission = rowType.children().first().text() : "";
	};
	
	this.updateFlightTrip = function() {
//		GDH Debut
		var dateBegin = $("#flight-gdh-begin-data").val().split("/");
		var timeHourBegin = $("#l_1_begin").val() != "" ? $("#l_1_begin").val() < 10?"0"+$("#l_1_begin").val(): $("#l_1_begin").val() : "00";
//		var timeHourBegin = "";
//		if ($("#l_1_begin").val() != "") {
//			
//		}
//		$("#l_1_begin").val() != "" ? $("#l_1_begin").val() : "00";
		
		var timeMinSecBegin = $("#l_3_begin").val()!= "" ? $("#l_3_begin").val()<10? "0"+$("#l_3_begin").val()+":00" : $("#l_3_begin").val()+":00" : "00:00";
//		var timeMinSecBegin = $("#l_3_begin").val()!= "" ? $("#l_3_begin").val()+":00" : "00:00";
		var gdhBegin = dateBegin[2] + "-" + dateBegin[1] + "-" + dateBegin[0] + " " + timeHourBegin + ":" + timeMinSecBegin;
		console.log("DATE BEGIN : " + gdhBegin);
		
//		GDH Fin
		var dateEnd = $("#flight-gdh-end-data").val().split("/");
		var timeHourEnd = $("#l_1_end").val() != "" ? $("#l_1_end").val() < 10?"0"+$("#l_1_end").val(): $("#l_1_end").val() : "00";
		var timeMinSecEnd = $("#l_3_end").val()!= "" ? $("#l_3_end").val()<10? "0"+$("#l_3_end").val()+":00" : $("#l_3_end").val()+":00" : "00:00";
		var gdhEnd = dateEnd[2] + "-" + dateEnd[1] + "-" + dateEnd[0] + " " + timeHourEnd + ":" + timeMinSecEnd;
		console.log("DATE END : " + gdhEnd);
		
		this.flight.gdhDebut != gdhBegin ? this.flight.gdhDebut = gdhBegin : "";
		this.flight.gdhFin != gdhEnd ? this.flight.gdhFin = gdhEnd : "";

		if (this.flight.listeDecollagesAtterrissages.length === 2) {
			var decollage = this.flight.listeDecollagesAtterrissages[0];
			var atterrissage = this.flight.listeDecollagesAtterrissages[1];
			
			decollage.gdh = gdhBegin;
			atterrissage.gdh = gdhEnd;
			decollage.lieuOACI = $("#oaci_depart").find("tr.active").first().length != 0 ? 
					decollage.lieuOACI = crvapp.stockageUtil.getLieuOACIObjectByIdPlateForm($("#oaci_depart").find("tr.active").first().attr("data-id")):"";
			atterrissage.lieuOACI = $("#oaci_fin").find("tr.active").first().length != 0 ? 
					atterrissage.lieuOACI = crvapp.stockageUtil.getLieuOACIObjectByIdPlateForm($("#oaci_fin").find("tr.active").first().attr("data-id")):"";
		} else {
			var decollage = new DecollageAtterissage();
			var atterrissage = new DecollageAtterissage();
			
			decollage.gdh = gdhBegin;
			atterrissage.gdh = gdhEnd;
			decollage.lieuOACI = $("#oaci_depart").find("tr.active").first().length != 0 ? 
					decollage.lieuOACI = crvapp.stockageUtil.getLieuOACIObjectByIdPlateForm($("#oaci_depart").find("tr.active").first().attr("data-id")): decollage.lieuOACI = "";
			atterrissage.lieuOACI = $("#oaci_fin").find("tr.active").first().length != 0 ? 
					decollage.lieuOACI = crvapp.stockageUtil.getLieuOACIObjectByIdPlateForm($("#oaci_fin").find("tr.active").first().attr("data-id")): decollage.lieuOACI = "";
					
			this.flight.listeDecollagesAtterrissages.push(decollage);
			this.flight.listeDecollagesAtterrissages.push(atterrissage);
		}
		
	};
	
	this.updateFlightHours = function() {
		this.flight.listeHeuresSAE.length = 0;
		var saeHoursList = new Array();
		$("#edit_crme_hours_list_tbody").find("tr").each(function() {
			var saeHour = new HeuresSAE();
			saeHour.serviceAerien = $(this).find("td.codeServiceAerien").first().text(); // A Valider et remplacer
			
			var tdDayValue =  $(this).find("td.dureeVolJour").first().text();
			var tdNightValue = $(this).find("td.dureeVolNuit").first().text();
			saeHour.dureeVolJour = tdDayValue.split(":")[0] * 60 * 60 + tdDayValue.split(":")[1] * 60;
			saeHour.dureeVolNuit = tdNightValue.split(":")[0] * 60 * 60 + tdNightValue.split(":")[1] * 60;
			saeHoursList.push(saeHour);
		});
		
		this.flight.listeHeuresSAE = saeHoursList;
	};
	
	this.loadValuesForEdit = function() {
		$edit = $('.demo_slide:visible');
		switch ($edit.attr("id")) {
		case "edit_flight_details":
			this.loadFlightDetails();
			break;
		case "edit_flight_consumption":
			this.loadFlightConsumption();
			break;
		case "edit_flight_intelligence":
			this.loadFlightIntelligence();
			break;
		case "edit_flight_trip":
			this.loadFlightTrip();
			break;
		default:
			break;
		}
	};
	
	this.loadFlightDetails = function() {
		var _this = this;
		if (!this.isNewFlight) {
			$("#demo_text_editor").text($("#id_display").text());
			
		} else {
			$("#demo_text_editor").text("Indicatif à saisir");
		}
		$("#unit_list_table").find("tr").each(function() {
			$(this).removeClass("active");
			if (_this.flight.unite.idUnite === $(this).attr("data-id")) {
				$(this).addClass("active");
			}
		});
		$("#flight_state_table").find("tr").each(function() {
			$(this).removeClass("active");
			if (_this.flight.etat === $(this).children("td").first().text()) {
				$(this).addClass("active");
			}
		});
		$("#category_flight_table").find("tr").each(function() {
			$(this).removeClass("active");
			if (_this.flight.categorie === $(this).children("td").first().text()) {
				$(this).addClass("active");
			}
		});
		$("#aeronef_flight_table").find("tr").each(function() {
			$(this).removeClass("active");
			if (_this.flight.aeronef.idAeronef === $(this).attr("data-id")) {
				$(this).addClass("active");
			}
		});
		$("#flight_rule_table").find("tr").each(function() {
			$(this).removeClass("active");
			if (_this.flight.regleVol === $(this).children("td").first().text()) {
				$(this).addClass("active");
			}
		});
		$("#flight_type_table").find("tr").each(function() {
			$(this).removeClass("active");
			if (_this.flight.typeVol === $(this).children("td").first().text()) {
				$(this).addClass("active");
			}
		});
	};
	
	this.loadFlightConsumption = function() {
//		this.flight.masseCarburantConsommee  != "" ? $("#slider-carburantConsomme-data").text(this.flight.masseCarburantConsommee) : $("#slider-carburantConsomme-data").text("0");
		this.flight.masseCarburantConsommee  != "" ? $("#slider-carburantConsomme-data").text(this.flight.masseCarburantConsommee) : $("#slider-carburantConsomme-data").text("0");
		$("#slider-carburantConsomme").slider("value", $("#slider-carburantConsomme-data").text());
//		this.flight.masseCarburantConsommee  != "" ? $("#carburantConsomme").val(this.flight.masseCarburantConsommee) : $("#carburantConsomme").val("0");
//		this.flight.masseCarburantConsommee  != "" ? $("#amount-fuel-consumption").val(this.flight.masseCarburantConsommee) : $("#amount-fuel-consumption").val("0");
	};
	

		this.loadFlightTrip = function() {
		if ($("#gdh_begin_display").text() != "" ) {
			var dateBegin = $("#gdh_begin_display").text().split(" ")[0];
			dateBegin = dateBegin.replace(/-/g, "/");
			$("#flight-gdh-begin").datepicker('setDate', dateBegin);
			var timeBegin = $("#gdh_begin_display").text().split(" ")[1];
			$("#l_1_begin").val(timeBegin.split(":")[0]);
			$("#l_3_begin").val(timeBegin.split(":")[1]);
		}
			
		if ($("#gdh_end_display").text() != "" ) {
			var dateEnd = $("#gdh_end_display").text().split(" ")[0];
			dateEnd = dateEnd.replace(/-/g, "/");
			$("#flight-gdh-end").datepicker('setDate', dateEnd);
			var timeEnd = $("#gdh_end_display").text().split(" ")[1];
			$("#l_1_end").val(timeEnd.split(":")[0]);
			$("#l_3_end").val(timeEnd.split(":")[1]);
		}
		
			var _this = this;
			$("#oaci_depart").find("tr").each(function() {
				$(this).removeClass("active");
				if (_this.flight.listeDecollagesAtterrissages[0].lieuOACI.idLieuOACI === $(this).attr("data-id")) {
					$(this).addClass("active");
				}
			});

			$("#oaci_fin").find("tr").each(function() {
				$(this).removeClass("active");
				if (_this.flight.listeDecollagesAtterrissages[1].lieuOACI.idLieuOACI === $(this).attr("data-id")) {
					$(this).addClass("active");
				}
			});
	};
	
	this.loadFlightIntelligence = function() {
		var _this = this;
		
		$("#mission_nature_list_tbody").find("tr").each(function() {
			$(this).removeClass("row_selected");
			if (_this.flight.natureMission === $(this).children("td").first().text()) {
				$(this).addClass("row_selected");
			}
		});
		
		$("#mission_manoeuvre_list").find("tr").each(function() {
			$(this).removeClass("row_selected");
			if (_this.flight.manoeuvreMission === $(this).children("td").first().text()) {
				$(this).addClass("row_selected");
			}
		});
		
		$("#mission_type_list").find("tr").each(function() {
			$(this).removeClass("row_selected");
			if (_this.flight.typeMission === $(this).children("td").first().text()) {
				$(this).addClass("row_selected");
			}
		});
	};
	
	this.loadDataForEditHours = function(domElement) {
		var row = $(domElement);
		$("#l_1_day_timespinner2").val(row.find("td.dureeVolJour").text().split(":")[0]);
		$("#l_3_day_timespinner2").val(row.find("td.dureeVolJour").text().split(":")[1]);
		$("#l_1_night_timespinner3").val(row.find("td.dureeVolNuit").text().split(":")[0]);
		$("#l_3_night_timespinner3").val(row.find("td.dureeVolNuit").text().split(":")[1]);
		
		$("#type_heure_tbody").find("tr").each(function() {
			$(this).removeClass("row_selected");
			if (row.find("td.codeServiceAerien").text() === $(this).children("td").first().text()) {
				$(this).addClass("row_selected");
			}
		});
	};
	
}